package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: chunlin
 * @date: 2020/10/9
 * @description
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Transactional
    @Override
    public void roleAdd(Role role, Integer[] menuIds, Integer[] permissionIds) {
        log.info("[角色-添加]data:{},menuIds:{},permissionIds:{}", role, menuIds, permissionIds);
        //插入基本数据
        roleDao.insert(role);
        //插入菜单数据
        for (Integer menuId : menuIds) {
            roleDao.insertRoleAndMenu(role.getId(), menuId);
        }
        //插入权限数据
        for (Integer permissionId : permissionIds) {
            roleDao.insertRoleAndPermission(role.getId(), permissionId);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data:{}", queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询page
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Set<Permission> findAllPermission() {
        return roleDao.selectAllPermission();
    }

    @Override
    public LinkedHashSet<Menu> findAllMenu() {
        return roleDao.selectAllMenu();
    }

    @Override
    public void roleDelete(Integer id) {
        log.info("[角色-根据id删除]id:{}", id);
        //校验是否可以删除
        Long count1 = roleDao.countRoleByMenuId(id);
        Long count2 = roleDao.countRoleByPermissionId(id);
        if (count1 > 0 || count2 > 0) {
            throw new BusinessRuntimeException("当前角色有数据, 不能删除");
        }
        //实际删除操作
        roleDao.deleteById(id);
    }

    @Transactional
    @Override
    public void roleEdit(Role role,
                         Integer[] menuIds,
                         Integer[] permissionIds) {
        log.info("[角色-编辑]data:{},menuIds:{},permissionIds:{}", role, menuIds, permissionIds);
        //更新角色
        roleDao.update(role);
        //删除原有的关系
        roleDao.deleteRoleAndMenuByRoleId(role.getId());
//        删除--关联关系
        roleDao.deleteRoleAndPermissionByRoleId(role.getId());
        //加入关联关系
        for (Integer menuId : menuIds) {
//
            roleDao.insertRoleAndMenu(role.getId(), menuId);
        }
        for (Integer permissionId : permissionIds) {
            roleDao.insertRoleAndPermission(role.getId(), permissionId);
        }
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.selectById(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.selectMenuIdsByRoleId(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.selectPermissionIdsByRoleId(id);
    }
}
